﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ZK.HardWareHelp
{
    public class AnReader
    {
        public AnReader(string ip, int port)
        {
            this.Ip = ip;
            this.Port = port;
        }
        private string _Ip;

        public string Ip
        {
            get { return _Ip; }
            set { _Ip = value; }
        }
        private int _Port;

        public int Port
        {
            get { return _Port; }
            set { _Port = value; }
        }


        TcpClient client;
        Stream streamToTran;
        private Thread waitThread;

        private bool bIsConnect = false;

        public bool Connect()
        {
            try
            {
                client = new TcpClient();
                client.Connect(this.Ip, this.Port);
                streamToTran = client.GetStream();    // 获取连接至远程的流

                //建立线程收取服务器发送数据
                ThreadStart stThead = new ThreadStart(ReceivedData);
                waitThread = new Thread(stThead);
                waitThread.IsBackground = true;
                waitThread.Start();

                bIsConnect = true;
                return true;
            }
            catch (System.Exception ex)
            {
                Common.Log4.LogManage.WriteErr("AnReader Connect " + ex.Message);
                return false;
            }
        }

        private void ReceivedData()
        {
            while (true)
            {
                try
                {
                    byte[] btAryBuffer = new byte[4096];
                    int nLenRead = streamToTran.Read(btAryBuffer, 0, btAryBuffer.Length);
                    if (nLenRead == 0)
                    {
                        continue;
                    }

                    byte[] btAryReceiveData = new byte[nLenRead];
                    Array.Copy(btAryBuffer, btAryReceiveData, nLenRead);
                    Common.DelegateHelper.MessageReceived?.Invoke(btAryReceiveData);

                    //if (Common.DelegateHelper.MessageReceived != null)
                    //{
                    //    byte[] btAryReceiveData = new byte[nLenRead];
                    //    Array.Copy(btAryBuffer, btAryReceiveData, nLenRead);
                    //    MessageReceived(btAryReceiveData);
                    //}
                }
                catch (System.Exception ex)
                {
                    Common.Log4.LogManage.WriteErr("AnReader ReceivedData " + ex.Message);
                }
            }

        }

        public bool SendMessage(byte[] btAryBuffer)
        {
            try
            {
                lock (streamToTran)
                {
                    streamToTran.Write(btAryBuffer, 0, btAryBuffer.Length);
                    return true;
                }
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("AnReader SendMessage " + ex.ToString());
                return false;
            }
        }

        public void SignOut()
        {
            if (streamToTran != null)
                streamToTran.Dispose();
            if (client != null)
                client.Close();

            waitThread.Abort();
            bIsConnect = false;
        }

        public bool IsConnect()
        {
            return bIsConnect;
        }
    }
}
